package com.quanquan.management.controller;

import DTO.PageDTO;
import DTO.store.StoreInfoDTO;
import base.BaseConst;
import com.quanquan.management.dto.SelfHelpIntegralDTO;
import com.quanquan.management.dto.UserDTO;
import com.quanquan.management.request.SelfHelpIntegralRequest;
import com.quanquan.management.service.SelfHelpIntegralService;
import com.quanquan.management.service.StoreService;
import com.quanquan.management.service.UserService;
import enums.ResCodeEnum;
import enums.UserResCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import web.response.ResultBody;

/**
 * @ClassName:SelfHelpIntegralController
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/4/15 20:58
 * @Version 1.0
 */
@Controller
@RequestMapping("/self_help_integral")
public class SelfHelpIntegralController extends ManagerBaseController{

    @Autowired
    private SelfHelpIntegralService selfHelpIntegralService;
    @Autowired
    private UserService userService;
    @Autowired
    private StoreService storeService;
    @GetMapping("/list")
    public String list(@RequestParam(required = false) Integer pageSize,
                       @RequestParam(required = false) Integer page,
                       @RequestParam(required = false) String keyword,
                       @RequestParam(required = false) String sort,
                       @RequestParam(required = false) String fromDate,
                       @RequestParam(required = false) String phone,
                       @RequestParam(required = false) String ticketNum,
                       @RequestParam(required = false) String toDate, Model model) {
        if (null == pageSize) {
            pageSize = 10;
        }
        if (null == page) {
            page = 0;
        } else if (page > 0){
            page = page -1;
        }

        SelfHelpIntegralRequest selfHelpIntegralRequest = new SelfHelpIntegralRequest();
        selfHelpIntegralRequest.setPage(page);
        selfHelpIntegralRequest.setSize(pageSize);
        selfHelpIntegralRequest.setKeyword(keyword);
        selfHelpIntegralRequest.setPhone(phone);
        selfHelpIntegralRequest.setTicketNum(ticketNum);
        selfHelpIntegralRequest.setToDate(toDate);
        selfHelpIntegralRequest.setFromDate(fromDate);
        selfHelpIntegralRequest.setAuditChannel("artificial");

        PageDTO<SelfHelpIntegralDTO> pageDTO = selfHelpIntegralService.list(selfHelpIntegralRequest);
        model.addAttribute("pageObj", pageDTO);
        return "selfHelpIntegral/list";
    }


    @GetMapping("/check_success_list")
    public String successList(@RequestParam(required = false) Integer pageSize,
                              @RequestParam(required = false) Integer page,
                              @RequestParam(required = false) String keyword,
                              @RequestParam(required = false) String sort,
                              @RequestParam(required = false) String fromDate,
                              @RequestParam(required = false) String phone,
                              @RequestParam(required = false) String ticketNum,
                              @RequestParam(required = false) String toDate, Model model) {
        if (null == pageSize) {
            pageSize = 10;
        }
        if (null == page) {
            page = 0;
        } else if (page > 0){
            page = page -1;
        }

        SelfHelpIntegralRequest selfHelpIntegralRequest = new SelfHelpIntegralRequest();
        selfHelpIntegralRequest.setPage(page);
        selfHelpIntegralRequest.setSize(pageSize);
        selfHelpIntegralRequest.setKeyword(keyword);
        selfHelpIntegralRequest.setPhone(phone);
        selfHelpIntegralRequest.setTicketNum(ticketNum);
        selfHelpIntegralRequest.setToDate(toDate);
        selfHelpIntegralRequest.setFromDate(fromDate);
        selfHelpIntegralRequest.setAuditChannel("check");
        PageDTO<SelfHelpIntegralDTO> pageDTO = selfHelpIntegralService.list(selfHelpIntegralRequest);
        model.addAttribute("pageObj", pageDTO);
        return "selfHelpIntegral/self_help_check_success_list.html";
    }


    /**
     * 添加
     * @param requestBody
     * @param model
     * @return
     */
    @RequestMapping("/add")
    public String add(SelfHelpIntegralRequest requestBody, Model model) {
        StoreInfoDTO storeInfoDTO = storeService.list();
        model.addAttribute("storeInfoDTO", storeInfoDTO);
        if (BaseConst.RequestMethod.GET.equals(this.request.getMethod())) {
            return "selfHelpIntegral/form";
        }
        int result = selfHelpIntegralService.addSelfHelpIntegral(requestBody);
        if (result==1) {
            model.addAttribute("isCommitted",true);
            return "selfHelpIntegral/form";
        }
        else if(result==2){
            model.addAttribute("error","请勿重复提交小票申请");
            model.addAttribute("editable",requestBody);
            return "selfHelpIntegral/form";
        }
        else if(result==4){
            model.addAttribute("error","没有选择商户无法提交小票申请");
            model.addAttribute("editable",requestBody);
            return "selfHelpIntegral/form";
        }
        else {
            model.addAttribute("error","用户不存在,保存失败");
            model.addAttribute("editable",requestBody);
            return "selfHelpIntegral/form";
        }
    }

    @GetMapping("/getByMobile")
    @ResponseBody
    public ResultBody getByMobile(@RequestParam String mobile) {
        UserDTO byId = userService.findByMobile(mobile);
        if (null != byId) {

            if(byId.getFreeze()==0){
                return new ResultBody(ResCodeEnum.SUCCESS.getCode(),ResCodeEnum.SUCCESS.getMsg(),byId);
            }else{
                return new ResultBody(UserResCodeEnum.FROZEN_ACCOUNT.getCode(),UserResCodeEnum.FROZEN_ACCOUNT.getMsg());
            }
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(),ResCodeEnum.FAIL.getMsg());
    }
    @RequestMapping("/export")
    public ResultBody exportData(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String fromDate,
            @RequestParam(required = false) String toDate,
            @RequestParam(required = false) Integer total,
            @RequestParam(required = false) String phone,
            @RequestParam(required = false) String ticketNum
    ) {
        SelfHelpIntegralRequest selfHelpIntegralRequest = new SelfHelpIntegralRequest();
        selfHelpIntegralRequest.setKeyword(keyword);
        selfHelpIntegralRequest.setSize(total);
        selfHelpIntegralRequest.setPhone(phone);
        selfHelpIntegralRequest.setTicketNum(ticketNum);
        selfHelpIntegralRequest.setFromDate(fromDate);
        selfHelpIntegralRequest.setToDate(toDate);
        try{
            selfHelpIntegralService.export(this.response,selfHelpIntegralRequest);
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(),ResCodeEnum.SUCCESS.getMsg());
        }catch (Exception e){
            e.printStackTrace();
            return new ResultBody(ResCodeEnum.FAIL.getCode(),ResCodeEnum.FAIL.getMsg());
        }
    }



    /**
     * 待审核申请列表
     * @param pageSize
     * @param page
     * @param keyword
     * @param sort
     * @param fromDate
     * @param phone
     * @param ticketNum
     * @param toDate
     * @param model
     * @return
     */
    @GetMapping("/check_list")
    public String checkApplicationList(@RequestParam(required = false) Integer pageSize,
                                       @RequestParam(required = false) Integer page,
                                       @RequestParam(required = false) String keyword,
                                       @RequestParam(required = false) String sort,
                                       @RequestParam(required = false) String fromDate,
                                       @RequestParam(required = false) String phone,
                                       @RequestParam(required = false) String ticketNum,
                                       @RequestParam(required = false) String toDate, Model model){

        if (null == pageSize) {
            pageSize = 10;
        }
        if (null == page) {
            page = 0;
        } else if (page > 0){
            page = page -1;
        }
        SelfHelpIntegralRequest selfHelpIntegralRequest = new SelfHelpIntegralRequest();
        selfHelpIntegralRequest.setPage(page);
        selfHelpIntegralRequest.setSize(pageSize);
        selfHelpIntegralRequest.setKeyword(keyword);
        selfHelpIntegralRequest.setPhone(phone);
        selfHelpIntegralRequest.setTicketNum(ticketNum);
        selfHelpIntegralRequest.setToDate(toDate);
        selfHelpIntegralRequest.setFromDate(fromDate);
        PageDTO<SelfHelpIntegralDTO> pageDTO = selfHelpIntegralService.checkList(selfHelpIntegralRequest);
        model.addAttribute("pageObj", pageDTO);

        return "selfHelpIntegral/self_help_review_list";
    }


    /**
     * 待审核申请列表(景枫特供版)
     * @param pageSize
     * @param page
     * @param fromDate
     * @param toDate
     * @param model
     * @return
     */
    @GetMapping("/check_list_jf")
    public String checkApplicationListJF(@RequestParam(required = false) Integer pageSize,
                                         @RequestParam(required = false) Integer page,
                                         @RequestParam(required = false) String fromDate,
                                         @RequestParam(required = false) String keyword,
                                         @RequestParam(required = false) String toDate, Model model){

        if (null == pageSize) {
            pageSize = 10;
        }
        if (null == page) {
            page = 0;
        } else if (page > 0){
            page = page -1;
        }
        SelfHelpIntegralRequest selfHelpIntegralRequest = new SelfHelpIntegralRequest();
        selfHelpIntegralRequest.setPage(page);
        selfHelpIntegralRequest.setSize(pageSize);
        selfHelpIntegralRequest.setToDate(toDate);
        selfHelpIntegralRequest.setFromDate(fromDate);
        selfHelpIntegralRequest.setKeyword(keyword);
        PageDTO<SelfHelpIntegralDTO> pageDTO = selfHelpIntegralService.checkListJF(selfHelpIntegralRequest);
        model.addAttribute("pageObj", pageDTO);

        return "selfHelpIntegral/new_self_help_review";
    }



    /**
     * 审核申请
     * @param requestBody
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String edit(String id, SelfHelpIntegralRequest requestBody, boolean error, Model model) {
        StoreInfoDTO storeInfoDTO = storeService.list();
        model.addAttribute("storeInfoDTO", storeInfoDTO);
        model.addAttribute("isError", error);
        if (BaseConst.RequestMethod.GET.equals(this.request.getMethod())) {
            SelfHelpIntegralDTO byId = selfHelpIntegralService.findById(id);
            model.addAttribute("editable",byId);
            return "selfHelpIntegral/new_self_help_review_form";
        }
        boolean update = selfHelpIntegralService.update(requestBody);
        if (update) {
            return "redirect:/self_help_integral/check_list_jf";
        }else {
            return "redirect:/self_help_integral/edit?error=true&id="+requestBody.getSelfHelpIntegralId();
        }
    }

    @GetMapping("/take_photo.html")
    public String takePhoto() {
        return "/common/take_photo.html";
    }

    /**
     * 审核成功和审核失败的申请列表
     * @param pageSize
     * @param page
     * @param keyword
     * @param sort
     * @param fromDate
     * @param phone
     * @param ticketNum
     * @param toDate
     * @param model
     * @return
     */
    @GetMapping("/review_result_list")
    public String reviewResultApplicationList(@RequestParam(required = false) Integer pageSize,
                                       @RequestParam(required = false) Integer page,
                                       @RequestParam(required = false) String keyword,
                                       @RequestParam(required = false) String sort,
                                       @RequestParam(required = false) String fromDate,
                                       @RequestParam(required = false) String phone,
                                       @RequestParam(required = false) String ticketNum,
                                       @RequestParam(required = false) String toDate, Model model){

        if (null == pageSize) {
            pageSize = 10;
        }
        if (null == page) {
            page = 0;
        } else if (page > 0){
            page = page -1;
        }
        SelfHelpIntegralRequest selfHelpIntegralRequest = new SelfHelpIntegralRequest();
        selfHelpIntegralRequest.setPage(page);
        selfHelpIntegralRequest.setSize(pageSize);
        selfHelpIntegralRequest.setKeyword(keyword);
        selfHelpIntegralRequest.setPhone(phone);
        selfHelpIntegralRequest.setTicketNum(ticketNum);
        selfHelpIntegralRequest.setToDate(toDate);
        selfHelpIntegralRequest.setFromDate(fromDate);
        PageDTO<SelfHelpIntegralDTO> pageDTO = selfHelpIntegralService.reviewResultList(selfHelpIntegralRequest);
        model.addAttribute("pageObj", pageDTO);

        return "selfHelpIntegral/self_help_status_list";
    }



}
